package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.exception.CustomException;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;
@Service
@Transactional
@Slf4j
public class  WmMaterialServiceImpl implements WmMaterialService {
    @Autowired
    private WmMaterialMapper wmMaterialMapper;
    @Autowired
    private FileStorageService fileStorageService;
    @Override
    public ResponseResult uploadFile(MultipartFile file) throws IOException {
        WmUser wmUser = WmThreadLocalUtil.getUser();

        if (null == wmUser){
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }

        String fileName = UUID.randomUUID().toString().replaceAll("-", "");
        String sub = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

        String url = fileStorageService.uploadHtmlFile("", fileName + sub, file.getInputStream());

        WmMaterial entity = new WmMaterial();
        entity.setCreatedTime(new Date());
        entity.setIsCollection((short)0);
        entity.setType((short)0);
        entity.setUserId(wmUser.getId());

        wmMaterialMapper.insert(entity);

        return ResponseResult.okResult(entity);
    }

    @Override
    public ResponseResult list(WmMaterialDto dto) {
        dto.checkParam();

        IPage page = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        if (dto.getIsCollection() != null){
            wrapper.eq(WmMaterial::getIsCollection, dto.getIsCollection());
        }

        WmUser wmUser = WmThreadLocalUtil.getUser();
        if (null == wmUser){
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }
        wrapper.eq(WmMaterial::getUserId, wmUser.getId());

        wmMaterialMapper.selectPage(page, wrapper);

        PageResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(), (int)page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;

    }
}
